package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import common.ServerResponse;
import service.IAddressService;
import service.impl.AddressServiceImpl;
import util.myUtil;

@WebServlet("/shipping")
public class AddressServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		myUtil.setHead(request, response);
		String type=request.getParameter("type");
		System.out.println("进入地址接口，此时的type:"+type);
		HttpSession session=request.getSession();
		Object id=session.getAttribute("id");
		if(id==null) {
			myUtil.getjson(ServerResponse.serverResponseByError("用户未登录，请登录！"), response);
			return;
		}
		String user_id=id.toString();
		//String user_id="1";
		if("add".equals(type)) {
			add(user_id,request, response);
		}else if("del".equals(type)) {
			del(user_id,request, response);
		}else if("update".equals(type)) {
			update(user_id,request, response);
		}else if("select".equals(type)) {
			select(user_id,request, response);
		}else if("list".equals(type)) {
			list(user_id,request, response);
		}
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	/***操作区****/
	/**************/
	protected void add(String user_id,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name=request.getParameter("receiver_name");
		String phone=request.getParameter("receiver_phone"); 
		String mobile=request.getParameter("receiver_mobile"); 
		String province=request.getParameter("receiver_province"); 
		String city=request.getParameter("receiver_city"); 
        String dis=request.getParameter("receiver_district");
		String zip=request.getParameter("receiver_zip");
		IAddressService a=new AddressServiceImpl();
		myUtil.getjson(a.add(user_id, name, phone, mobile, province, city,dis,zip), response);
		
	}
	
	protected void del(String user_id,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String address_id=request.getParameter("address_id");
		IAddressService a=new AddressServiceImpl();
		myUtil.getjson(a.del(address_id), response);
	}
    
	protected void update(String user_id,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String address_id=request.getParameter("id");
		String name=request.getParameter("receiver_name");
		String phone=request.getParameter("receiver_phone"); 
		String mobile=request.getParameter("receiver_mobile"); 
		String province=request.getParameter("receiver_province"); 
		String city=request.getParameter("receiver_city"); 
		 String dis=request.getParameter("receiver_district");
		String zip=request.getParameter("receiver_zip");
		IAddressService a=new AddressServiceImpl();
		myUtil.getjson(a.update(address_id, name, phone, mobile, province, city, dis, zip), response);
	}
	protected void select(String user_id,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String address_id=request.getParameter("address_id");
		IAddressService a=new AddressServiceImpl();
		myUtil.getjson(a.select(address_id), response);
	}
	protected void list(String user_id,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pageNum=request.getParameter("pageNum");
		String pageSize=request.getParameter("pageSize");
		IAddressService a=new AddressServiceImpl();
		myUtil.getjson(a.list(user_id, pageNum, pageSize), response);
	}

}
